<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Api\V1;

use App\Services\FileService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Controller(prefix: '/api/v1/file/')]
class FileController extends CController
{
    #[Inject]
    protected FileService $fileService;

    /**
     * 上传文件.
     */
    #[RequestMapping(path: 'upload', methods: 'post')]
    public function upload()
    {
        $params = $this->request->all();
        $params['file'] = $file = $this->request->file('file');
        $params['user'] = $this->user();
        $this->validate($params, [
            'file' => 'required|file',
        ]);
        $data = $this->fileService->uploadFile($params['file'], $params['avatar'] ?? false);
        return $this->success($data, $this->request);
    }

    /**
     * 上传文件base64.
     */
    #[RequestMapping(path: 'upload-base64', methods: 'post')]
    public function uploadBase64()
    {
        $params = $this->request->all();
        $params['file'] = $file = $this->request->file('file');
        $params['user'] = $this->user();
        $this->validate($params, [
            'base64' => 'required',
        ]);
        $data = $this->fileService->uploadBase64($params['base64']);
        return $this->success($data, $this->request);
    }
}
